package com.hexb.smh.entity.param.product;

import com.hexb.core.utils.ObjectUtils;
import com.hexb.smh.entity.ProductPrice;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;

import static com.hexb.smh.utils.SmhConstant.AMOUNT_PATTERN;

/**
 * @author : hexb
 */
@Data
public class ProductPriceParam {

    @ApiModelProperty(value = "品牌Id", example = "1")
    private Integer brandId;

    @ApiModelProperty(value = "固定成本价", example = "10")
    @NotNull
    @Pattern(regexp = AMOUNT_PATTERN)
    private String fixedCostPrice;

    @ApiModelProperty(value = "可变成本价", example = "12")
    @NotNull
    @Pattern(regexp = AMOUNT_PATTERN)
    private String volatileCostPrice;

    @ApiModelProperty(value = "其它成本价", example = "13")
    @NotNull
    @Pattern(regexp = AMOUNT_PATTERN)
    private String otherCostPrice;

    @ApiModelProperty(value = "市场价", example = "100")
    @Pattern(regexp = AMOUNT_PATTERN)
    private String marketPrice;

    public ProductPrice to() {
        ProductPrice p = new ProductPrice();
        p.setBrandId(brandId);
        p.setFixedCostPrice(ObjectUtils.safeCall(() -> new BigDecimal(fixedCostPrice), null));
        p.setVolatileCostPrice(ObjectUtils.safeCall(() -> new BigDecimal(volatileCostPrice), null));
        p.setOtherCostPrice(ObjectUtils.safeCall(() -> new BigDecimal(otherCostPrice), null));
        if (!StringUtils.isEmpty(marketPrice)) {
            p.setMarketPrice(ObjectUtils.safeCall(() -> new BigDecimal(marketPrice), null));
        }
        return p;
    }
}
